/*
 * author      : 吴中勤
 * date        : 2019/3/18
 * description : TODO
 */

package org.wzq.learn.threadpool;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.*;

public class ThreadLocalThreadPoolExecutor extends ThreadPoolExecutor {
    private static final Logger logger = LoggerFactory.getLogger(ThreadLocalThreadPoolExecutor.class);

    public ThreadLocalThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    public ThreadLocalThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
    }

    public ThreadLocalThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
    }

    public ThreadLocalThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
    }

    // 执行 before时, 在新的线程中
    @Override
    protected void beforeExecute(Thread t, Runnable r) {
        if (logger.isInfoEnabled()) logger.info("beforeExecute, threadId=" + Thread.currentThread().getId());
        AppRuntimeEnvThread thread = (AppRuntimeEnvThread) t;
        // clean threadLocal
        thread.clean();
        // To properly nest multiple overridings, subclasses
        // should generally invoke {@code super.beforeExecute}
        // at the end of this method
        super.beforeExecute(t, r);
    }

    // 执行 after 时, 在新的线程中
    @Override
    protected void afterExecute(Runnable r, Throwable t) {
        super.afterExecute(r, t);

        if (logger.isInfoEnabled()) logger.info("afterExecute, threadId=" + Thread.currentThread().getId());
        // throw new RuntimeException(t);
    }
}
